package fr.insee.rome.io.mots;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import fr.insee.rome.factory.RomeFactory;
import fr.insee.rome.string.comp.StringComparator;

public class MapMots implements Mots{

	private Map<String, Set<String>> mots;
	private StringComparator comparator = null;
	
	private static Mots instance = null;
	
	private MapMots(){
		mots = new HashMap<String, Set<String>>();
		comparator = RomeFactory.getComparator();
	}
	
	public static Mots getInstance(){
		if(instance == null){
			instance = new MapMots();
		}
		return instance;
	}
	
	public void add(String mot, String code){
		
		if(!mots.containsKey(mot)){
			mots.put(mot, new HashSet<String>());
		}
		mots.get(mot).add(code);
	}
	
	public Set<String> get(String mot){
		return mots.get(mot);
	}

	public Iterator<String> iterator() {
		return mots.keySet().iterator();
	}
	
	public Set<String> find(String mot){
		
		Set<String> codes = new HashSet<String>();
		for (String key : this) {
			if(comparator.matches(key, mot)){
				codes.addAll(this.get(key));
			}
		}
		return codes;
	}
}
